<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- qsyntaxhighlighter.cpp -->
  <title>QSyntaxHighlighter Class | Qt GUI 5.14.2</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.14</a></td><td ><a href="qtgui-index.html">Qt GUI</a></td><td ><a href="qtgui-module.html">C++ Classes</a></td><td >QSyntaxHighlighter</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtgui-index.html">Qt 5.14.2 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#public-slots">Public Slots</a></li>
<li class="level1"><a href="#protected-functions">Protected Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QSyntaxHighlighter Class</h1>
<!-- $$$QSyntaxHighlighter-brief -->
<p>The QSyntaxHighlighter class allows you to define syntax highlighting rules, and in addition you can use the class to query a document's current formatting or user data. <a href="#details">More...</a></p>
<!-- @@@QSyntaxHighlighter -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> Header:</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QSyntaxHighlighter&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += gui</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Since:</td><td class="memItemRight bottomAlign"> Qt 4.1</td></tr><tr><td class="memItemLeft rightAlign topAlign"> Inherits:</td><td class="memItemRight bottomAlign"> <a href="../qtcore/qobject.html">QObject</a></td></tr></table></div><p>This class was introduced in Qt 4.1.</p>
<ul>
<li><a href="qsyntaxhighlighter-members.html">List of all members, including inherited members</a></li>
</ul>
<p><b>Note:</b> All functions in this class are <a href="../qtdoc/threads-reentrancy.html">reentrant</a>.</p>
<a name="public-functions"></a>
<h2 id="public-functions">Public Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#QSyntaxHighlighter-1">QSyntaxHighlighter</a></b>(QTextDocument *<i>parent</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#QSyntaxHighlighter">QSyntaxHighlighter</a></b>(QObject *<i>parent</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#dtor.QSyntaxHighlighter">~QSyntaxHighlighter</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextDocument *</td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#document">document</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setDocument">setDocument</a></b>(QTextDocument *<i>doc</i>)</td></tr>
</table></div>
<a name="public-slots"></a>
<h2 id="public-slots">Public Slots</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#rehighlight">rehighlight</a></b>()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#rehighlightBlock">rehighlightBlock</a></b>(const QTextBlock &amp;<i>block</i>)</td></tr>
</table></div>
<a name="protected-functions"></a>
<h2 id="protected-functions">Protected Functions</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> QTextBlock </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#currentBlock">currentBlock</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextBlockUserData *</td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#currentBlockUserData">currentBlockUserData</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QTextCharFormat </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#format">format</a></b>(int <i>position</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a></b>(const QString &amp;<i>text</i>) = 0</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a></b>(int <i>newState</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a></b>(QTextBlockUserData *<i>data</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setFormat">setFormat</a></b>(int <i>start</i>, int <i>count</i>, const QTextCharFormat &amp;<i>format</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setFormat-1">setFormat</a></b>(int <i>start</i>, int <i>count</i>, const QColor &amp;<i>color</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qsyntaxhighlighter.html#setFormat-2">setFormat</a></b>(int <i>start</i>, int <i>count</i>, const QFont &amp;<i>font</i>)</td></tr>
</table></div>
<a name="details"></a>
<!-- $$$QSyntaxHighlighter-description -->
<div class="descr">
<h2 id="details">Detailed Description</h2>
<p>The QSyntaxHighlighter class is a base class for implementing <a href="qtextdocument.html">QTextDocument</a> syntax highlighters. A syntax highligher automatically highlights parts of the text in a <a href="qtextdocument.html">QTextDocument</a>. Syntax highlighters are often used when the user is entering text in a specific format (for example source code) and help the user to read the text and identify syntax errors.</p>
<p>To provide your own syntax highlighting, you must subclass QSyntaxHighlighter and reimplement <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<p>When you create an instance of your QSyntaxHighlighter subclass, pass it the <a href="qtextdocument.html">QTextDocument</a> that you want the syntax highlighting to be applied to. For example:</p>
<pre class="cpp">

  <span class="type"><a href="../qtwidgets/qtextedit.html">QTextEdit</a></span> <span class="operator">*</span>editor <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qtextedit.html">QTextEdit</a></span>;
  MyHighlighter <span class="operator">*</span>highlighter <span class="operator">=</span> <span class="keyword">new</span> MyHighlighter(editor<span class="operator">-</span><span class="operator">&gt;</span>document());

</pre>
<p>After this your <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() function will be called automatically whenever necessary. Use your <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() function to apply formatting (e.g&#x2e; setting the font and color) to the text that is passed to it. QSyntaxHighlighter provides the <a href="qsyntaxhighlighter.html#setFormat">setFormat</a>() function which applies a given <a href="qtextcharformat.html">QTextCharFormat</a> on the current text block. For example:</p>
<pre class="cpp">

  <span class="type">void</span> MyHighlighter<span class="operator">::</span>highlightBlock(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text)
  {
      <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> myClassFormat;
      myClassFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);
      myClassFormat<span class="operator">.</span>setForeground(<span class="type">Qt</span><span class="operator">::</span>darkMagenta);

      <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> expression(<span class="string">&quot;\\bMy[A-Za-z]+\\b&quot;</span>);
      <span class="type"><a href="../qtcore/qregularexpressionmatchiterator.html">QRegularExpressionMatchIterator</a></span> i <span class="operator">=</span> expression<span class="operator">.</span>globalMatch(text);
      <span class="keyword">while</span> (i<span class="operator">.</span>hasNext())
      {
        <span class="type"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> match <span class="operator">=</span> i<span class="operator">.</span>next();
        setFormat(match<span class="operator">.</span>capturedStart()<span class="operator">,</span> match<span class="operator">.</span>capturedLength()<span class="operator">,</span> myClassFormat);
      }
  }

</pre>
<a name="qsyntaxhighlighter-multiblock"></a><p>Some syntaxes can have constructs that span several text blocks. For example, a C++ syntax highlighter should be able to cope with <code>/</code><code>*..&#x2e;*</code><code>/</code> multiline comments. To deal with these cases it is necessary to know the end state of the previous text block (e.g&#x2e; &quot;in comment&quot;).</p>
<p>Inside your <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() implementation you can query the end state of the previous text block using the <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() function. After parsing the block you can save the last state using <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<p>The <a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a>() and <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() functions return an int value. If no state is set, the returned value is -1. You can designate any other value to identify any given state using the <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>() function. Once the state is set the <a href="qtextblock.html">QTextBlock</a> keeps that value until it is set set again or until the corresponding paragraph of text is deleted.</p>
<p>For example, if you're writing a simple C++ syntax highlighter, you might designate 1 to signify &quot;in comment&quot;:</p>
<pre class="cpp">

  <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> multiLineCommentFormat;
  multiLineCommentFormat<span class="operator">.</span>setForeground(<span class="type">Qt</span><span class="operator">::</span>red);

  <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> startExpression(<span class="string">&quot;/\\*&quot;</span>);
  <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> endExpression(<span class="string">&quot;\\*/&quot;</span>);

  setCurrentBlockState(<span class="number">0</span>);

  <span class="type">int</span> startIndex <span class="operator">=</span> <span class="number">0</span>;
  <span class="keyword">if</span> (previousBlockState() <span class="operator">!</span><span class="operator">=</span> <span class="number">1</span>)
      startIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(startExpression);

  <span class="keyword">while</span> (startIndex <span class="operator">&gt;</span><span class="operator">=</span> <span class="number">0</span>) {
     <span class="type"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> endMatch;
     <span class="type">int</span> endIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(endExpression<span class="operator">,</span> startIndex<span class="operator">,</span> <span class="operator">&amp;</span>endMatch);
     <span class="type">int</span> commentLength;
     <span class="keyword">if</span> (endIndex <span class="operator">=</span><span class="operator">=</span> <span class="operator">-</span><span class="number">1</span>) {
         setCurrentBlockState(<span class="number">1</span>);
         commentLength <span class="operator">=</span> text<span class="operator">.</span>length() <span class="operator">-</span> startIndex;
     } <span class="keyword">else</span> {
         commentLength <span class="operator">=</span> endIndex <span class="operator">-</span> startIndex
                         <span class="operator">+</span> endMatch<span class="operator">.</span>capturedLength();
     }
     setFormat(startIndex<span class="operator">,</span> commentLength<span class="operator">,</span> multiLineCommentFormat);
     startIndex <span class="operator">=</span> text<span class="operator">.</span>indexOf(startExpression<span class="operator">,</span>
                               startIndex <span class="operator">+</span> commentLength);
  }

</pre>
<p>In the example above, we first set the current block state to 0. Then, if the previous block ended within a comment, we highlight from the beginning of the current block (<code>startIndex = 0</code>). Otherwise, we search for the given start expression. If the specified end expression cannot be found in the text block, we change the current block state by calling <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>(), and make sure that the rest of the block is highlighted.</p>
<p>In addition you can query the current formatting and user data using the <a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#currentBlockUserData">currentBlockUserData</a>() functions respectively. You can also attach user data to the current text block using the <a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a>() function. <a href="qtextblockuserdata.html">QTextBlockUserData</a> can be used to store custom settings. In the case of syntax highlighting, it is in particular interesting as cache storage for information that you may figure out while parsing the paragraph's text. For an example, see the <a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a>() documentation.</p>
</div>
<p><b>See also </b><a href="qtextdocument.html">QTextDocument</a> and <a href="../qtwidgets/qtwidgets-richtext-syntaxhighlighter-example.html">Syntax Highlighter Example</a>.</p>
<!-- @@@QSyntaxHighlighter -->
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QSyntaxHighlighter$$$QSyntaxHighlighterQTextDocument* -->
<h3 class="fn" id="QSyntaxHighlighter-1"><a name="QSyntaxHighlighter-1"></a>QSyntaxHighlighter::<span class="name">QSyntaxHighlighter</span>(<span class="type"><a href="qtextdocument.html">QTextDocument</a></span> *<i>parent</i>)</h3>
<p>Constructs a QSyntaxHighlighter and installs it on <i>parent</i>. The specified <a href="qtextdocument.html">QTextDocument</a> also becomes the owner of the QSyntaxHighlighter.</p>
<!-- @@@QSyntaxHighlighter -->
<!-- $$$QSyntaxHighlighter[overload1]$$$QSyntaxHighlighterQObject* -->
<h3 class="fn" id="QSyntaxHighlighter"><a name="QSyntaxHighlighter"></a>QSyntaxHighlighter::<span class="name">QSyntaxHighlighter</span>(<span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i>)</h3>
<p>Constructs a QSyntaxHighlighter with the given <i>parent</i>.</p>
<p>If the parent is a <a href="../qtwidgets/qtextedit.html">QTextEdit</a>, it installs the syntax highlighter on the parents document. The specified <a href="../qtwidgets/qtextedit.html">QTextEdit</a> also becomes the owner of the QSyntaxHighlighter.</p>
<!-- @@@QSyntaxHighlighter -->
<!-- $$$rehighlight[overload1]$$$rehighlight -->
<h3 class="fn" id="rehighlight"><a name="rehighlight"></a><code>[slot] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">rehighlight</span>()</h3>
<p>Reapplies the highlighting to the whole document.</p>
<p>This function was introduced in Qt 4.2.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#rehighlightBlock">rehighlightBlock</a>().</p>
<!-- @@@rehighlight -->
<!-- $$$rehighlightBlock[overload1]$$$rehighlightBlockconstQTextBlock& -->
<h3 class="fn" id="rehighlightBlock"><a name="rehighlightBlock"></a><code>[slot] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">rehighlightBlock</span>(const <span class="type"><a href="qtextblock.html">QTextBlock</a></span> &amp;<i>block</i>)</h3>
<p>Reapplies the highlighting to the given <a href="qtextblock.html">QTextBlock</a> <i>block</i>.</p>
<p>This function was introduced in Qt 4.6.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#rehighlight">rehighlight</a>().</p>
<!-- @@@rehighlightBlock -->
<!-- $$$~QSyntaxHighlighter[overload1]$$$~QSyntaxHighlighter -->
<h3 class="fn" id="dtor.QSyntaxHighlighter"><a name="dtor.QSyntaxHighlighter"></a><code>[virtual] </code>QSyntaxHighlighter::<span class="name">~QSyntaxHighlighter</span>()</h3>
<p>Destructor. Uninstalls this syntax highlighter from the text document.</p>
<!-- @@@~QSyntaxHighlighter -->
<!-- $$$currentBlock[overload1]$$$currentBlock -->
<h3 class="fn" id="currentBlock"><a name="currentBlock"></a><code>[protected] </code><span class="type"><a href="qtextblock.html">QTextBlock</a></span> QSyntaxHighlighter::<span class="name">currentBlock</span>() const</h3>
<p>Returns the current text block.</p>
<p>This function was introduced in Qt 4.4.</p>
<!-- @@@currentBlock -->
<!-- $$$currentBlockState[overload1]$$$currentBlockState -->
<h3 class="fn" id="currentBlockState"><a name="currentBlockState"></a><code>[protected] </code><span class="type">int</span> QSyntaxHighlighter::<span class="name">currentBlockState</span>() const</h3>
<p>Returns the state of the current text block. If no value is set, the returned value is -1.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<!-- @@@currentBlockState -->
<!-- $$$currentBlockUserData[overload1]$$$currentBlockUserData -->
<h3 class="fn" id="currentBlockUserData"><a name="currentBlockUserData"></a><code>[protected] </code><span class="type"><a href="qtextblockuserdata.html">QTextBlockUserData</a></span> *QSyntaxHighlighter::<span class="name">currentBlockUserData</span>() const</h3>
<p>Returns the <a href="qtextblockuserdata.html">QTextBlockUserData</a> object previously attached to the current text block.</p>
<p><b>See also </b><a href="qtextblock.html#userData">QTextBlock::userData</a>() and <a href="qsyntaxhighlighter.html#setCurrentBlockUserData">setCurrentBlockUserData</a>().</p>
<!-- @@@currentBlockUserData -->
<!-- $$$document[overload1]$$$document -->
<h3 class="fn" id="document"><a name="document"></a><span class="type"><a href="qtextdocument.html">QTextDocument</a></span> *QSyntaxHighlighter::<span class="name">document</span>() const</h3>
<p>Returns the <a href="qtextdocument.html">QTextDocument</a> on which this syntax highlighter is installed.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#setDocument">setDocument</a>().</p>
<!-- @@@document -->
<!-- $$$format[overload1]$$$formatint -->
<h3 class="fn" id="format"><a name="format"></a><code>[protected] </code><span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> QSyntaxHighlighter::<span class="name">format</span>(<span class="type">int</span> <i>position</i>) const</h3>
<p>Returns the format at <i>position</i> inside the syntax highlighter's current text block.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#setFormat">setFormat</a>().</p>
<!-- @@@format -->
<!-- $$$highlightBlock[overload1]$$$highlightBlockconstQString& -->
<h3 class="fn" id="highlightBlock"><a name="highlightBlock"></a><code>[pure virtual protected] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">highlightBlock</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>text</i>)</h3>
<p>Highlights the given text block. This function is called when necessary by the rich text engine, i.e&#x2e; on text blocks which have changed.</p>
<p>To provide your own syntax highlighting, you must subclass <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> and reimplement highlightBlock(). In your reimplementation you should parse the block's <i>text</i> and call <a href="qsyntaxhighlighter.html#setFormat">setFormat</a>() as often as necessary to apply any font and color changes that you require. For example:</p>
<pre class="cpp">

  <span class="type">void</span> MyHighlighter<span class="operator">::</span>highlightBlock(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text)
  {
      <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> myClassFormat;
      myClassFormat<span class="operator">.</span>setFontWeight(<span class="type"><a href="qfont.html">QFont</a></span><span class="operator">::</span>Bold);
      myClassFormat<span class="operator">.</span>setForeground(<span class="type">Qt</span><span class="operator">::</span>darkMagenta);

      <span class="type"><a href="../qtcore/qregularexpression.html">QRegularExpression</a></span> expression(<span class="string">&quot;\\bMy[A-Za-z]+\\b&quot;</span>);
      <span class="type"><a href="../qtcore/qregularexpressionmatchiterator.html">QRegularExpressionMatchIterator</a></span> i <span class="operator">=</span> expression<span class="operator">.</span>globalMatch(text);
      <span class="keyword">while</span> (i<span class="operator">.</span>hasNext())
      {
        <span class="type"><a href="../qtcore/qregularexpressionmatch.html">QRegularExpressionMatch</a></span> match <span class="operator">=</span> i<span class="operator">.</span>next();
        setFormat(match<span class="operator">.</span>capturedStart()<span class="operator">,</span> match<span class="operator">.</span>capturedLength()<span class="operator">,</span> myClassFormat);
      }
  }

</pre>
<p>See the <a href="qsyntaxhighlighter.html#qsyntaxhighlighter-multiblock">Detailed Description</a> for examples of using <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>(), <a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a>() and <a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>() to handle syntaxes with constructs that span several text blocks</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#previousBlockState">previousBlockState</a>(), <a href="qsyntaxhighlighter.html#setFormat">setFormat</a>(), and <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<!-- @@@highlightBlock -->
<!-- $$$previousBlockState[overload1]$$$previousBlockState -->
<h3 class="fn" id="previousBlockState"><a name="previousBlockState"></a><code>[protected] </code><span class="type">int</span> QSyntaxHighlighter::<span class="name">previousBlockState</span>() const</h3>
<p>Returns the end state of the text block previous to the syntax highlighter's current block. If no value was previously set, the returned value is -1.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() and <a href="qsyntaxhighlighter.html#setCurrentBlockState">setCurrentBlockState</a>().</p>
<!-- @@@previousBlockState -->
<!-- $$$setCurrentBlockState[overload1]$$$setCurrentBlockStateint -->
<h3 class="fn" id="setCurrentBlockState"><a name="setCurrentBlockState"></a><code>[protected] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">setCurrentBlockState</span>(<span class="type">int</span> <i>newState</i>)</h3>
<p>Sets the state of the current text block to <i>newState</i>.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#currentBlockState">currentBlockState</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setCurrentBlockState -->
<!-- $$$setCurrentBlockUserData[overload1]$$$setCurrentBlockUserDataQTextBlockUserData* -->
<h3 class="fn" id="setCurrentBlockUserData"><a name="setCurrentBlockUserData"></a><code>[protected] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">setCurrentBlockUserData</span>(<span class="type"><a href="qtextblockuserdata.html">QTextBlockUserData</a></span> *<i>data</i>)</h3>
<p>Attaches the given <i>data</i> to the current text block. The ownership is passed to the underlying text document, i.e&#x2e; the provided <a href="qtextblockuserdata.html">QTextBlockUserData</a> object will be deleted if the corresponding text block gets deleted.</p>
<p><a href="qtextblockuserdata.html">QTextBlockUserData</a> can be used to store custom settings. In the case of syntax highlighting, it is in particular interesting as cache storage for information that you may figure out while parsing the paragraph's text.</p>
<p>For example while parsing the text, you can keep track of parenthesis characters that you encounter ('{[(' and the like), and store their relative position and the actual <a href="../qtcore/qchar.html">QChar</a> in a simple class derived from <a href="qtextblockuserdata.html">QTextBlockUserData</a>:</p>
<pre class="cpp">

  <span class="keyword">struct</span> ParenthesisInfo
  {
      <span class="type"><a href="../qtcore/qchar.html">QChar</a></span> <span class="type">char</span>;
      <span class="type">int</span> position;
  };

  <span class="keyword">struct</span> BlockData : <span class="keyword">public</span> <span class="type"><a href="qtextblockuserdata.html">QTextBlockUserData</a></span>
  {
      <span class="type"><a href="../qtcore/qvector.html">QVector</a></span><span class="operator">&lt;</span>ParenthesisInfo<span class="operator">&gt;</span> parentheses;
  };

</pre>
<p>During cursor navigation in the associated editor, you can ask the current <a href="qtextblock.html">QTextBlock</a> (retrieved using the <a href="qtextcursor.html#block">QTextCursor::block</a>() function) if it has a user data object set and cast it to your <code>BlockData</code> object. Then you can check if the current cursor position matches with a previously recorded parenthesis position, and, depending on the type of parenthesis (opening or closing), find the next opening or closing parenthesis on the same level.</p>
<p>In this way you can do a visual parenthesis matching and highlight from the current cursor position to the matching parenthesis. That makes it easier to spot a missing parenthesis in your code and to find where a corresponding opening/closing parenthesis is when editing parenthesis intensive code.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#currentBlockUserData">currentBlockUserData</a>() and <a href="qtextblock.html#setUserData">QTextBlock::setUserData</a>().</p>
<!-- @@@setCurrentBlockUserData -->
<!-- $$$setDocument[overload1]$$$setDocumentQTextDocument* -->
<h3 class="fn" id="setDocument"><a name="setDocument"></a><span class="type">void</span> QSyntaxHighlighter::<span class="name">setDocument</span>(<span class="type"><a href="qtextdocument.html">QTextDocument</a></span> *<i>doc</i>)</h3>
<p>Installs the syntax highlighter on the given <a href="qtextdocument.html">QTextDocument</a> <i>doc</i>. A <a href="qsyntaxhighlighter.html">QSyntaxHighlighter</a> can only be used with one document at a time.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#document">document</a>().</p>
<!-- @@@setDocument -->
<!-- $$$setFormat[overload1]$$$setFormatintintconstQTextCharFormat& -->
<h3 class="fn" id="setFormat"><a name="setFormat"></a><code>[protected] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">setFormat</span>(<span class="type">int</span> <i>start</i>, <span class="type">int</span> <i>count</i>, const <span class="type"><a href="qtextcharformat.html">QTextCharFormat</a></span> &amp;<i>format</i>)</h3>
<p>This function is applied to the syntax highlighter's current text block (i.e&#x2e; the text that is passed to the <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>() function).</p>
<p>The specified <i>format</i> is applied to the text from the <i>start</i> position for a length of <i>count</i> characters (if <i>count</i> is 0, nothing is done). The formatting properties set in <i>format</i> are merged at display time with the formatting information stored directly in the document, for example as previously set with <a href="qtextcursor.html">QTextCursor</a>'s functions. Note that the document itself remains unmodified by the format set through this function.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setFormat -->
<!-- $$$setFormat$$$setFormatintintconstQColor& -->
<h3 class="fn" id="setFormat-1"><a name="setFormat-1"></a><code>[protected] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">setFormat</span>(<span class="type">int</span> <i>start</i>, <span class="type">int</span> <i>count</i>, const <span class="type"><a href="qcolor.html">QColor</a></span> &amp;<i>color</i>)</h3>
<p>This is an overloaded function.</p>
<p>The specified <i>color</i> is applied to the current text block from the <i>start</i> position for a length of <i>count</i> characters.</p>
<p>The other attributes of the current text block, e.g&#x2e; the font and background color, are reset to default values.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setFormat -->
<!-- $$$setFormat$$$setFormatintintconstQFont& -->
<h3 class="fn" id="setFormat-2"><a name="setFormat-2"></a><code>[protected] </code><span class="type">void</span> QSyntaxHighlighter::<span class="name">setFormat</span>(<span class="type">int</span> <i>start</i>, <span class="type">int</span> <i>count</i>, const <span class="type"><a href="qfont.html">QFont</a></span> &amp;<i>font</i>)</h3>
<p>This is an overloaded function.</p>
<p>The specified <i>font</i> is applied to the current text block from the <i>start</i> position for a length of <i>count</i> characters.</p>
<p>The other attributes of the current text block, e.g&#x2e; the font and background color, are reset to default values.</p>
<p><b>See also </b><a href="qsyntaxhighlighter.html#format">format</a>() and <a href="qsyntaxhighlighter.html#highlightBlock">highlightBlock</a>().</p>
<!-- @@@setFormat -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
